<template>
    <el-form class="upload-form" ref="form" :model="form" label-width="100px" text-align="center">
      <el-form-item label="appName">
        <el-input v-model="form.appName" :placeholder="apkInfo.appName"></el-input>
      </el-form-item>
      <el-form-item label="versionName">
        <el-input v-model="form.versionName" :placeholder="apkInfo.latestVersionName"></el-input>
      </el-form-item>
      <el-form-item label="forceCode">
        <el-input v-model="form.forceCode" :placeholder="apkInfo.latestForceCode"></el-input>
      </el-form-item>
      <el-form-item label="versionCode">
        <el-input v-model="form.versionCode" :placeholder="apkInfo.latestVersionCode"></el-input>
      </el-form-item>
      <el-form-item label="upgradeInfo">
        <el-input type="textarea" v-model="form.upgradeInfo" autosize :rows="5"></el-input>
      </el-form-item>
      <el-upload class="upload-demo" drag action :http-request="httpRequest" :before-upload="beforeUpload"
        :on-exceed="handleExceed" :limit="1" :file-list="fileList" accept=".apk" multiple v-loading="loading"
        element-loading-text="apk文件上传中">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传apk文件,且不能超过500mb</div>
      </el-upload>
      <div class="buttons">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button>取消</el-button>
        <el-button class="downloadLatest" type="success" @click="onDownload">下载最新版本apk</el-button>
      </div>
    </el-form>
</template>
<script>
import { uploadApk, checkLatestApk } from '@/api/apk'
export default {
  data() {
    return {
      loading: false,
      form: {
        appName: '',
        versionName: '',
        forceCode: '',
        versionCode: '',
        upgradeInfo: ''
      },
      fileList: [],
      apkInfo: {},
      downloadUrl: 'http://' + window.location.hostname + ":8878"
    }
  },
  created() {
    const res = checkLatestApk()
    res.then((apkInfo) => {
      this.apkInfo = apkInfo
      this.downloadUrl += apkInfo.updateUrl
    })
  },
  methods: {
    onSubmit() {
      // 使用form表单的数据格式
      this.loading = true
      const params = new FormData()
      params.append("file", this.fileList[0])
      for (var key in this.form) {
        params.append(key, this.form[key]);
      }
      uploadApk(params).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        }
        else {
          this.$message.error(res.msg)
        }
        this.loading = false     
        location.reload()
      })
    },
    onDownload() {
      window.location.replace(this.downloadUrl)
    },
    httpRequest(option) {
      this.fileList.push(option.file)
    },
    beforeUpload(file) {
      let fileSize = file.size
      const FIVE_M = 500 * 1024 * 1024;
      //大于500M，不允许上传
      if (fileSize > FIVE_M) {
        this.$message.error("最大上传500M的apk")
        return false
      }
      //判断文件类型，必须是格式
      let fileName = file.name
      let reg = /^.+(\.apk)$/
      if (!reg.test(fileName)) {
        this.$message.error("只能上传.apk文件!")
        return false
      }
      return true
    },
    // 文件数量过多时提醒
    handleExceed() {
      this.$message({ type: 'error', message: '最多支持1个apk上传' })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
.upload-form {
  position: absolute;
  left: 50%;
  width: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  min-width: 400px;
  height: auto !important;
  min-height: 400px;
  background-color: rgba(255, 255, 255, 0.9);
  padding: 50px 20px 10px 20px;
  font-size: 24px;
  font-weight: 600;
  border-radius: 10px;
  align-items: center;
}

.el-upload {
  width: 100%;

  .el-upload-dragger {
    width: 100%;
    min-width: 360px;
    height: 150px;

    .el-icon-upload {
      margin: 20px 0 16px;
    }
  }
}

.buttons {
  margin-top: 10px;
  text-align: center;
}</style>
